<template>
    <el-form ref="form" size="small" label-width="120px">
        <!--<el-form-item label="模块类型">-->
            <!--<el-radio-group v-model="form.svc_module_type">-->
                <!--<el-radio :label="1">数据模块个性化</el-radio>-->
                <!--<el-radio :label="2">微接口模块</el-radio>-->
            <!--</el-radio-group>-->
        <!--</el-form-item>-->
        <el-form-item label="模块名称">
            <el-input v-model="form.func_module_name" placeholder="名称，如：人员相关函数"></el-input>
        </el-form-item>
        <el-form-item label="模块包名">
            <el-input v-model="form.func_module_code" placeholder="编码，如：adminModule，命名请使用全英文，云函数的文件名"></el-input>
        </el-form-item>
        <el-form-item label="" label-width="0px" style="text-align: center;margin-top: 30px;">
            <el-button size="default" type="primary"
                       @click="handleSubmitCreateGroup()"> 提交 </el-button>
            <el-button size="default" @click="handleCancel()"> 取消 </el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import _ from 'lodash';
    import common from '../func/common';

    const DEFAULT_FORM = {
        // svc_module_id: '',
        task_code: '',
        task_name: '',
        app_id: -1,
        type: null
    }

    export default {
        props: [ 'currentData', 'app' ],
        data() {
            return {
                loading: false,
                actionType: 'view',     // createModule | createMenu | edit | view
                form: _.cloneDeep(DEFAULT_FORM),
                rules: {
                    func_module_code: [
                        {required: true, message: '请输入函数模块编码', trigger: 'blur'}
                    ],
                    func_module_name: [
                        {required: true, message: '请输入函数模块名称', trigger: 'blur'}
                    ]
                }
            }
        },
        components: {

        },
        created () {

        },
        mounted () {
            this.init();
        },
        watch: {
            'currentData': {
                deep: true,
                handler: function () {
                    this.init();
                }
            },
            app: {
                deep: true,
                handler: function () {
                    this.init();
                }
            },
            'actionType'() {
                this.init();
            }
        },
        filters: {

        },
        computed: {

        },
        methods: {
            ...common,
            init() {
                let form = null;
                if(this.currentData && this.currentData.task_id) {
                    form = _.cloneDeep(this.currentData);
                } else {
                    let appId = _.get(this.currentData, 'id');
                    let type = 3;

                    if(this.isPlatform(this.currentData)) {
                        type = 1;
                    } else if(this.isGlobal(this.currentData)) {
                        type = 2;
                    } else if(this.isApp(this.currentData)) {
                        type = 3;
                    }

                    form = _.defaults({
                        func_module_code: '',
                        func_module_name: '',
                        app_id: appId > 0 ? appId: -1,
                        type
                    }, DEFAULT_FORM);
                }

                this.form = form;
            },
            async handleSubmitCreateGroup() {

                let data = _.cloneDeep(this.form);
                let id = _.get(data, 'func_module_id');
                let ret = null;

                if(id) {
                    ret = await this.$japi.task.update(id, data);
                } else {
                    ret = await this.$japi.task.create(data);
                }

                let tid = _.get(this.form, 'tid') || `M_${_.get(ret, 'func_module_id')}`;

                this.loading = false;

                if(ret) {
                    this.$message('操作成功！');
                    this.handleCancel(tid);
                }
            },
            handleCancel(tid) {
                this.$emit('close', tid);
            },
        }
    }
</script>
